package service.impl;

import dao.MeetingDao;
import model.Meeting;
import pojo.PageEntity;
import service.MeetingService;
import utils.PageUtil;

import java.util.Date;
import java.util.List;

public class MeetingServiceImpl implements MeetingService {

    private MeetingDao meetingDao=new MeetingDao(Meeting.class);
    @Override
    public PageEntity<Meeting> findPage(Integer currPage, String title, Integer status) {

        PageEntity<Meeting> pageModel = new PageEntity<>();
        Integer pageSize = pageModel.getPageSize();
        List<Meeting> data = meetingDao.findPage(title, status, (currPage-1)*pageSize, pageSize);
        Integer totalCount = countByTitleAndStatus(title, status);
        pageModel = PageUtil.createMeetPage(currPage, data, totalCount);
        return pageModel;
    }

    @Override
    public Integer countByTitleAndStatus(String title, Integer status) {
        return meetingDao.countByTitleAndStatus(title,status);
    }

    @Override
    public void save(Meeting meeting) {
        meeting.setStatus(0);
        meeting.setPublishDate(new Date());
        meetingDao.save(meeting);
    }

    @Override
    public Meeting findById(Integer id) {
        return meetingDao.findById(id);
    }

    @Override
    public Integer findMeetingCountByMeetingId(Integer id) {
        return meetingDao.findMeetingCountByMeetingId(id);
    }

    @Override
    public Boolean checkJoin(Integer uId, Integer id) {
        Integer count = meetingDao.checkJoin(uId, id);
        return count != 0 ? true : false;
    }

    @Override
    public void unJoinMeeting(Integer uId, Integer id) {
        meetingDao.unJoinMeeting(uId, id);
    }

    @Override
    public void joinMeeting(Integer uId, Integer id) {
        meetingDao.joinMeeting(uId, id);
    }
}
